<?php


namespace api\project\controller;


use api\project\model\LoginModel;
use api\project\model\SmsCodeModel;
use api\project\validate\UserValidate;
use cmf\controller\RestBaseController;
use think\facade\Filesystem;
use think\Request;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class UserController extends RestBaseController
{
        //注册api
        public function register(Request $request)
        {
            $data = $request->param();
            //验证器
            $admin = LoginModel::where('mobile','=',$data['mobile'])
                ->find();
            if(empty($admin))
            {
                $this->error('该手机号已注册');
            }
            $validate = new UserValidate();
            $val = $validate->scene('register')->check($data);
            if(!$val)
            {
                $this->error($validate->getError());
            }

            //验证验证码
            CheckSmsCode($data['mobile'], $data['code']);
            //入库
            $res = LoginModel::create($data);
            if($res)
            {
               $this->success('注册成功');
            }else{
                $this->error('注册失败');
            }
        }
        //登录api
        public function login(Request $request)
        {
            $mobile = $request->param('mobile');
            $code = $request->param('code');

            if(empty($mobile))
            {
                $this->error('手机号码不能为空');
            }
            if(empty($code)){
                $this->error('验证码不能为空');
            }
            $res = LoginModel::where('mobile','=',$mobile)
                ->find();
            if(empty($res))
            {
                $this->error('手机号错误');
            }
            $sms = SmsCodeModel::where('mobile','=',$mobile)
                ->find();
            if(CheckSmsCode($mobile, $sms['code']))
            {
                $token = sendToken($res['id'],$mobile);
                $this->success(
                    '登录成功',
                    [
                        'data'=>$token
                    ]
                );
            }else{
                $this->error('登录失败');
            }
        }
        public function SentCode(Request $request)
        {
            $mobile = $request->param('mobile');
            //发送验证码
            sendAliSms($mobile);
        }
        //查找
        public function SelectAdmin(Request $request)
        {
            $mobile = $request->param('mobile');
            if(empty($mobile))
            {
                $this->error('手机号为空');
            }
            $res = LoginModel::where('mobile','=',$mobile)
                ->find();
            $this->success('用户信息',
            [
                'res'=>$res
            ]);
        }
        //上传图片
        public function SentImage(Request $request)
        {
            $image = $request->file('image');
            $image = Filesystem::disk('public')->putFile('image', $image);
            if($image)
            {
                $this->success('图片地址',
                ['image'=>$request->domain().'/strong'.$image]);
            }
        }
    //七牛云
        public function uploadToQiniu(Request $request)
        {
            $accessKey = Config('app.qiniuoss.accessKey');
            $secretKey = Config('app.qiniuoss.secretKey');
            $bucket = Config('app.qiniuoss.bucket');
            $file = $request->file('file');
            if (!empty($file)) {
                // 构建鉴权对象
                $auth = new Auth($accessKey, $secretKey);
                // 生成上传 Token
                $token = $auth->uploadToken($bucket);
                // 要上传文件的本地路径
                $filePath = $file->getPathname();
                // 上传到七牛后保存的文件名
                $key = time().'.'.$file->getOriginalName();
                // 初始化 UploadManager 对象并进行文件的上传。
                $uploadMgr = new UploadManager();
                // 调用 UploadManager 的 putFile 方法进行文件的上传。
                list($return, $err) = $uploadMgr->putFile($token, $key, $filePath);
                if ($err !== null) {
                    return $err; // 失败
                } else {
                    return $key; // 成功，$key 就是在七牛云的文件名，加上你在七牛云的外链地址就可以访问到文件
                }
            }
        }
    //更新信息
        public function UpdateAdmin(Request $request)
        {
            $user_name = $request->param('user_name');
            $mobile = $request->param('mobile');
            $image = $request->param('image');
            $res = LoginModel::where('mobile','=',$mobile)
            ->update([
                'user_image'=>$image,
                'name'=>$user_name
            ]);
            if(!empty($res))
            {
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
}